import 'dart:convert';

import 'package:crypto/crypto.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import '../../../data/api/api.dart';

class SignUpController extends GetxController {

  TextEditingController accountTec = new TextEditingController();
  TextEditingController pwdTec = new TextEditingController();
  TextEditingController nicknameTec = new TextEditingController();

  Api api = new Api();

  Future<void> signUp() async {
    if (accountTec.text.length < 11 || accountTec.text.length > 12 || pwdTec.text.length < 6  || nicknameTec.text.length == 0) {
      Get.snackbar("提示", "填写信息有误！");
      return;
    }
    var encodePwd = md5.convert(utf8.encode(pwdTec.text)).toString();
    var r = await api.signUp(accountTec.text, encodePwd, nicknameTec.text);
    if (!r) {
      Get.snackbar("提示", "注册失败");
    } else {
      EasyLoading.showSuccess("注册成功!");
      Get.back();
    }
  }

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {}
}
